package com.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 封装 Map 方便添加响应数据
 */
@Data
public class HttpClientMapResult extends HashMap implements Serializable {

    public HttpClientMapResult() {}

    public HttpClientMapResult(int code) {
        this(code, null);
    }

    public HttpClientMapResult(int code, Map<String, Object> data) {
        setStatusCode(code);
        if (data != null) putAll(data);
    }

    /**
     * @Description 注意 key 不要有其它特殊符号啥的，在返回数据时会转换 JSON 失败从而变成 String 字符串
     * @param key
     * @param obj
     * @return
     */
    @JsonIgnore //不需要返回的 get 方法
    public HttpClientMapResult putParam(String key, Object obj) {
        put(key, obj);
        return this;
    }

    @JsonIgnore
    public HttpClientMapResult setStatusCode(int code) {
        put("code", code);
        return this;
    }

    @JsonIgnore
    public HttpClientMapResult putSuccessOrFailedMsg(boolean res) {
        setStatusCode(res ? 200 : -1);
        put(res ? "success" : "failed", res ? "成功" : "失败");
        return this;
    }

    @JsonIgnore
    public HttpClientMapResult putMsg(Object obj) {
        put("msg", obj);
        return this;
    }

    @JsonIgnore
    public HttpClientMapResult putErrMsg(Object obj) {
        put("errMsg", obj);
        return this;
    }

    @JsonIgnore
    public HttpClientMapResult putToken(Object obj) {
        put("token", obj);
        return this;
    }

}
